package com.springCrud.simpleCrud.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.ClassEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.DataBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.springCrud.simpleCrud.entity.SimpleCrud;
import com.springCrud.simpleCrud.service.SimpleCrudService;
import com.springCrud.simpleCrud.util.Constraint;
import com.springCrud.simpleCrud.util.OrderMap;
import com.springCrud.simpleCrud.util.PagingInfo;
import com.springCrud.simpleCrud.util.SearchResult;
import com.springCrud.simpleCrud.util.FlashMap.MessageType;

@Controller
@RequestMapping("/simpleCrud/*")
public class SimpleCrudController extends BaseController {
	
	@Autowired private SimpleCrudService simpleCrudService;
	
	protected final String VIEW_FOLDER = "hse/inquiry";

	@InitBinder
	public void initBinder(WebDataBinder dataBinder) {
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
		
	}
	
	protected void specificSearch(Object object, StringBuffer buffer,
			Map<String, Object> parameters, String operator, String prefix, String suffix) {
		
		SimpleCrud simpleCrud = (SimpleCrud) object;
		
    	boolean isAppendAnd = false;
    	if (StringUtils.isNotBlank(simpleCrud.getName())) {
			buffer.append("simpleCrud.name" + " LIKE " + " :name");
			parameters.put("name", prefix + simpleCrud.getName() + suffix);
			
			isAppendAnd = true;
		}
		if (StringUtils.isNotBlank(simpleCrud.getAddress())) {
			if(isAppendAnd) {
				buffer.append(" and ");
			}
			buffer.append("simpleCrud.address " + " LIKE " + " :address");
			parameters.put("address", prefix + simpleCrud.getAddress() + suffix);
			
			isAppendAnd = true;
		} 
	}
	
	@RequestMapping("/search")
	public String search(@ModelAttribute SimpleCrud simpleCrud, @ModelAttribute PagingInfo pagingInfo, ModelMap modelMap) {
		
		OrderMap orderMap = new OrderMap();
    	orderMap.put("simpleCrud.id", "asc");
    	
    	Constraint constraint = resolveSearch(null, simpleCrud);
    	
    	if (!StringUtils.equals(constraint.getWhereClause(), "")) {
			constraint.setWhereClause(constraint.getWhereClause() + " and ");
		}
		constraint.setWhereClause(constraint.getWhereClause() + " simpleCrud.deleted = 0 ");
    	
		SearchResult searchResult = simpleCrudService.searchSimpleCrud(constraint, orderMap,
    			true, pagingInfo.getOffset(), 5);
		
        modelMap.put("simpleCruds", searchResult.getRecs());
        modelMap.put("pagingInfo", searchResult.getPagingInfo());
        
		return "/simpleCrud";
	}
	
	@RequestMapping(value="/open", method=RequestMethod.GET)
	public String open(ModelMap modelMap, @RequestParam(required=false) Long id) {
		
		SimpleCrud simpleCrud = new SimpleCrud();
		
		if (id != null && id.intValue() != 0) {
			simpleCrud = simpleCrudService.findSimpleCrudById(id);
		}
		
		modelMap.put("simpleCrud", simpleCrud);
		
		return "/simpleCrud_detail";
	}
	
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public String save(@RequestParam(required=false) String act, @RequestParam(required=false) String reason, ModelMap modelMap, @ModelAttribute SimpleCrud simpleCrud, BindingResult result) {
		
		simpleCrud.setDeleted(0);
		
		simpleCrudService.save(simpleCrud);
			
		addMessage(modelMap, "Data berhasil disimpan.", MessageType.success, true);
		
		return "redirect:open?id="+ simpleCrud.getId();
	}
	
	@RequestMapping(value="/delete", method=RequestMethod.GET)
	public String delete(ModelMap modelMap, @RequestParam(required=false) Long id) {
		
		SimpleCrud simpleCrud = new SimpleCrud();
		
		simpleCrud = simpleCrudService.findSimpleCrudById(id);
		
		simpleCrud.setDeleted(1);
		
		simpleCrudService.save(simpleCrud);
		
		addMessage(modelMap, "Data berhasil dihapus.", MessageType.success, true);
		
		return "redirect:search";
	}
}
